import {ref} from "vue";
import type {
    DtoCtrlPlan,
    DtoFailMode4requirementList,
    DtoRequirementDetails,
    Frequency,
    ReactionPlan,
} from "./type.ts";
import api from "@/utils/api";
import {message} from "ant-design-vue";

export const saving = ref(false);

export const requirementDetails = ref<DtoRequirementDetails>({
    id: undefined,
    roleCaseId: undefined,
    value: '',
    department: {},
    role: {},
    business: {},
    dtoFailMode4requirementList: [{
        fmDesc:undefined,
        ctrlPlans:[{
            frequencyList:[],
            reactionPlanList:[],
        }],
    }],
});

export const resetRequirementDetails = () => {
    requirementDetails.value = {
        id: undefined,
        roleCaseId: undefined,
        value: '',
        department: {},
        role: {},
        business: {},
        dtoFailMode4requirementList: [{
            fmDesc:undefined,
            ctrlPlans:[{
                frequencyList:[],
                reactionPlanList:[],
            }],
        }],
    };
};

export const initializeFromRecord = async (record: any, mode: string) => {
    resetRequirementDetails();
    if (mode === 'preview') {
        if(record) {
            const iFms=ref<DtoFailMode4requirementList[]>([]);
            //用record数据填充iFms容器
            if(record.dtoFailMode4requirementList){
                record.dtoFailMode4requirementList.forEach((fm:DtoFailMode4requirementList) => {
                    const iCps=ref<DtoCtrlPlan[]>([]);
                    //用record数据填充iCps容器
                    if(fm.ctrlPlans){
                        fm.ctrlPlans.forEach(cp => {
                            const iFqs=ref<Frequency[]>([]);
                            //用record数据填充iFqs容器
                            if(cp.frequencyList){
                                cp.frequencyList.forEach(fq => {
                                    iFqs.value.push({
                                        id: fq.id,
                                        cnDesc: fq.cnDesc,
                                    });
                                });
                            }
                            const iRps=ref<ReactionPlan[]>([]);
                            //用record数据填充iRps容器
                            if(cp.reactionPlanList){
                                cp.reactionPlanList.forEach(rp => {
                                    iRps.value.push({
                                        id: rp.id,
                                        cnDesc: rp.cnDesc,
                                    });
                                });
                            }
                            iCps.value.push({
                                cpId:cp.cpId,
                                cpSpec:cp.cpSpec,
                                capacityId:cp.capacityId,
                                capacityCn:cp.capacityCn,
                                frequencyList:iFqs.value,
                                reactionPlanList:iRps.value,
                            });
                        });
                    }
                    iFms.value.push({
                        fmId:fm.fmId,
                        fmDesc:fm.fmDesc,
                        ctrlPlans:iCps.value,
                    });
                });
            }
            //用record数据填充requirementDetails容器
            requirementDetails.value={
                id: record.id,
                roleCaseId: record.roleCaseId,
                value: record.value,
                department: {
                    id:record.dpmId,
                    nameCn:record.dpmNameCn,
                },
                role: {
                    id:record.roleId,
                    nameCn:record.roleName,
                },
                business: {
                    id:record.businessId,
                    name:record.businessName,
                    roleCaseId:record.roleCaseId,
                },
                dtoFailMode4requirementList: iFms.value,
            }
        }
    }
};

export const handleSave = async () => {
    try {
        saving.value = true;

        await api.post('/requirement/dtoRequirementDetails', requirementDetails.value);

        message.success(`保存成功`);
        return true;
    } catch (error) {
        message.error('保存失败');
        console.error('保存失败:', error);
        return false;
    } finally {
        saving.value = false;
    }
};